/**
 *
 * The MIT License
 *
 * Copyright (c) 2008 Board of Trustees, Leland Stanford Jr. University. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.macdadi.core.client.ui;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Hyperlink;

/**
 * I'd like to optionally turn off History participation, but this is not
 * easily possible because of various private (instead of protected) fields
 * in Hyperlink. Instead, I'm trying to let one turn off the "#" behaviour
 * by specifying an empty token value.
 *
 * @author Ben Suter <ben.suter@gmail.com>
 */
public class GeneralHyperlink extends Hyperlink {

  /**
   * Sets the history token referenced by this hyperlink. This is the history
   * token that will be passed to {@link com.google.gwt.user.client.History#newItem} when this link is
   * clicked. 
   * 
   * If the argument is the empty string, no fragment portion will be appended
   * to the href value.
   * 
   * @param targetHistoryToken the new target history token
   *
   */
    public void setTargetHistoryToken(String targetHistoryToken) {
        super.setTargetHistoryToken(targetHistoryToken);
        if ( "".equals(targetHistoryToken) )
            DOM.setElementProperty(getAnchorElem(), "href", ""); // empty instead of "#token"
    }
    
    protected Element getAnchorElem() {
        /* Note: this relies on the implementation details of Hyperlink and could thus break. */
        return DOM.getChild(getElement(), 0);
    }


}